package com.ctg.payroll2.service.cusadmin.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ctg.payroll2.bean.CsOrgchartTree;
import com.ctg.payroll2.bean.po.CsOrgchart;
import com.ctg.payroll2.dao.cusadmin.ICsOrgchartDAO;
import com.ctg.payroll2.service.cusadmin.IOrgStructureManageService;

public class OrgStructureManageServiceImpl implements
		IOrgStructureManageService {
	private Log logger = LogFactory.getLog(getClass());
	private static final Integer VALID = 1;
	private static final Integer INVALID = 0;
	private static final int CREATE = 1;
	private static final int UPDATE = 2;
	private static final int DELETE = 3;
	private static final int ErrHasValidSubOrganization = -1;
	private static final int ErrHasValidEmployee = -2;
	private ICsOrgchartDAO csOrgchartDAO;
	
	public ICsOrgchartDAO getCsOrgchartDAO() {
		return csOrgchartDAO;
	}

	public void setCsOrgchartDAO(ICsOrgchartDAO csOrgchartDAO) {
		this.csOrgchartDAO = csOrgchartDAO;
	}
	
	public List<CsOrgchart> getSubOrganization(Integer organizationId) throws SQLException{
		Map<String, Object> parameters = new HashMap<String, Object>();
		if(organizationId != null){
			parameters.put("orgchart_id", organizationId.intValue());
		}else{
			return null;
		}
		parameters.put("is_valid", VALID);
		List<CsOrgchart> resultlist = csOrgchartDAO.queryForList(".getSubOrganization", parameters);
		for(int i = 0;i < resultlist.size();i++){
			((CsOrgchart) resultlist.get(i)).setIsLeaf(isLeaf(((CsOrgchart) resultlist.get(i)).getOrgchart_id()));
		}
		return resultlist;
	}
	
	public CsOrgchart getRootOrganization(String company_code) throws SQLException{
		Map<String, Object> parameters = new HashMap<String, Object>();
		if(company_code != null){
			parameters.put("company_code", company_code);
		}else{
			return null;
		}
		CsOrgchart orgroot = csOrgchartDAO.queryForObject(".getRoot", parameters);
		orgroot.setIsLeaf(isLeaf(orgroot.getOrgchart_id()));
		return orgroot;
	}
	
	public CsOrgchart getOrganizationInfo(Integer organizationId) throws SQLException{
		Map<String, Object> parameters = new HashMap<String, Object>();
		if(organizationId != null){
			parameters.put("orgchart_id", organizationId.intValue());
		}else{
			return null;
		}
		parameters.put("is_valid", VALID);
		CsOrgchart org = csOrgchartDAO.queryForObject(".getOrganization", parameters);
		org.setIsLeaf(isLeaf(org.getOrgchart_id()));
		return org;
	}
	
	public CsOrgchart getParentOrganization(Integer organizationId) throws SQLException{
		return null;
	}
	
	public int modifyOrganization(CsOrgchart organization, Integer operation) throws SQLException{
//		System.out.println("Begin edit process...");
		if(organization != null && operation != null){
			switch (operation.intValue()){
			case CREATE:
//				System.out.println("Begin CREATE operation ...");
				csOrgchartDAO.insert(organization);
				break;
			case UPDATE:
//				System.out.println("Begin UPDATE operation ...");
				csOrgchartDAO.update(organization);
				break;
			case DELETE:
//				System.out.println("Begin DELETE operation ...");
				if(isLeaf(organization.getOrgchart_id())){
					if(getEmployees(organization.getOrgchart_id(), VALID).size()>0){
						return ErrHasValidEmployee;
					}else if(getEmployees(organization.getOrgchart_id(), INVALID).size()>0){
						organization.setSuper_org_id(null);
						csOrgchartDAO.update("disableOrganization", organization);
					}else{
						csOrgchartDAO.deleteByPrimaryKey(organization.getOrgchart_id());
					}
				}else{
					return ErrHasValidSubOrganization;
				}
				break;
			default:
				break;
			}
		}
		return 0;
	}
	
	public List getSimpleCsOrg(String company_code) throws SQLException {
		return csOrgchartDAO.queryForList(".selectForSimple", company_code);
	}
	
	public CsOrgchart addOrganization(CsOrgchart organization) throws SQLException{
//		System.out.println("Begin add new organization process...");
		if(organization != null){
			csOrgchartDAO.insert(organization);
		}else{
			return null;
		}
		return organization;
	}
	
	public CsOrgchart editOrganization(CsOrgchart organization) throws SQLException{
//		System.out.println("Begin edit process...");
		if(organization != null){
			csOrgchartDAO.update(organization);
		}else{
			return null;
		}
		return organization;
	}
	
	public int delOrganization(CsOrgchart organization) throws SQLException{
//		System.out.println("Begin delete process...");
		boolean leaf = false;
		try{
			leaf = isLeaf(organization.getOrgchart_id());
		}catch(SQLException se){
			se.printStackTrace();
		}
		if(organization != null){
				if(leaf){
					if(getEmployees(organization.getOrgchart_id(), VALID).size()>0){
						return ErrHasValidEmployee;
					}else if(getEmployees(organization.getOrgchart_id(), INVALID).size()>0){
						organization.setSuper_org_id(null);
						csOrgchartDAO.update("disableOrganization", organization);
					}else{
						csOrgchartDAO.deleteByPrimaryKey(organization.getOrgchart_id());
					}
				}else{
					return ErrHasValidSubOrganization;
				}
		}
		return 0;
	}
	
	private boolean isLeaf(Integer organizationId) throws SQLException{
		Map<String, Object> parameters = new HashMap<String, Object>();
		if(organizationId != null){
			parameters.put("orgchart_id", organizationId.intValue());
		}else{
			return false;
		}
		if(csOrgchartDAO.queryForList(".getSubOrganization", parameters).size()>0){
			return false;
		}else{
			return true;
		}
	}
	
	private List getEmployees(Integer organizationId, Integer isvalid){
		if(isvalid != null){
			return new ArrayList();
		}else{
			return new ArrayList();
		}
		
	}
	
	public CsOrgchartTree getOrgTree(String company_code) throws SQLException {
		CsOrgchartTree tree = null;
		if (company_code == null) {
			return tree;
		}
		Map<String,Object> parameters =new HashMap<String,Object>();
		parameters.put("company_code", company_code);
		CsOrgchart root = csOrgchartDAO.queryForObject(".getRoot", parameters);
		if (root != null) {
			tree = new CsOrgchartTree(root);
			//parameters.put("orgchart_id", tree.getId());
			List<CsOrgchart> childrens = csOrgchartDAO.queryForList(".getChildren", tree.getId());
			Iterator<CsOrgchart> items = childrens.iterator();
			while (items.hasNext()) {
				CsOrgchart c = items.next();
//				System.out.println(c.getOrgchart_name());
				if (c.getSuper_org_id() == null) {
					continue;
				}
				CsOrgchartTree r = tree.findChindren(c.getSuper_org_id());
				if (r != null) {
					r.addChild(new CsOrgchartTree(c));
				}
			}
		}
		return tree;
	}

}
